<?php
header("Content-Type:text/html; charset=utf-8");
define('DBHOST', '127.0.0.1');
define('DBUSER', 'root');
define('DBPW', '');
define('DBNAME', 'landrover');
$con = mysql_connect(DBHOST, DBUSER, DBPW);
if (!$con) {
	die('Could not connect: ' . mysql_error()); 
}
$db_selected = mysql_select_db(DBNAME, $con);
mysql_query('SET NAMES UTF8');
$type=empty($_POST['type'])?0:$_POST['type'];
switch($type)
{
	case 1:
		$mobile=empty($_POST['mobile'])?'':$_POST['mobile'];
		$sql = "SELECT mobile FROM foton20140107 WHERE mobile='$mobile' LIMIT 1";
		$result = mysql_query($sql,$con);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		if($row)
		{
			echo 1;
		}
		else
		{
			echo 0;	
		}
		break;
		
	case 2:
		$name = empty($_POST['name'])?'':$_POST['name'];
		$mobile = empty($_POST['mobile'])?'':$_POST['mobile'];
		$province = empty($_POST['province'])?'':$_POST['province'];
		$city = empty($_POST['city'])?'':$_POST['city'];
		$ip = empty($_POST['ip'])?'':$_POST['ip'];
		$cartype = empty($_POST['cartype'])?'':$_POST['cartype'];
		$logtime = empty($_POST['logtime'])?'':$_POST['logtime'];
		$channel = empty($_POST['channel'])?'':$_POST['channel'];
		$sql = "INSERT INTO foton20140107 (username, mobile,province, city, ip, cartype,logtime,channel) VALUES('$name', '$mobile','$province', '$city', '$ip', '$cartype','$logtime','$channel')";
		$result = mysql_query($sql,$con);
		if ($result)
		{
			echo 1;
		} 
		else 
		{
			echo 0;
		}
		break;
		
	case 3:
		$sql = "SELECT mobile,jiangpin FROM foton20140107 WHERE jiangpin>0 AND jiangpin<7 ORDER BY id DESC LIMIT 0,10";
		$result = mysql_query($sql,$con);
		$str = '';
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
		{
			switch($row['jiangpin'])
			{
				case '1':
					$jiangpin='一等奖';
					break;
				case '2':
					$jiangpin='二等奖';
					break;
				case '3':
					$jiangpin='三等奖';
					break;
				case '4':
					$jiangpin='四等奖';
					break;
				case '5':
					$jiangpin='五等奖';
					break;
				case '6':
					$jiangpin='六等奖';
			}
			$fourNum=substr($row['mobile'], 3,4);
			$row['mobile']=preg_replace("/$fourNum/", '****', $row['mobile'],1);
			$str.='<li>手机号为'.$row['mobile'].'获得了'.$jiangpin.'</li>';
		}
		echo $str;
		break;
	
	case 4:
		$mobile = empty($_POST['mobile'])?'':$_POST['mobile'];
		$sql = "SELECT jiangpin,cartype FROM foton20140107 WHERE mobile='$mobile' LIMIT 1";
		$result = mysql_query($sql,$con);
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		echo json_encode($row);
		break;
		
	case 5:
		$mobile = empty($_POST['mobile'])?'':$_POST['mobile'];
		$luck_num = empty($_POST['luck_num'])?0:$_POST['luck_num'];
		$sql = "UPDATE foton20140107 SET jiangpin='$luck_num' WHERE mobile='$mobile'";
		mysql_query($sql,$con);
		if(mysql_affected_rows())
		{
			echo 1;	
		}
		else
		{
			echo 0;	
		}
		break;
		
	case 6:
		$channel=empty($_POST['channel'])?'':trim($_POST['channel']);
		$sql = 'SELECT * FROM foton20140107';             
		$order = ' ORDER BY id DESC'; 
		$where='';
		if(!empty($channel)){
			$where .= ' WHERE channel = '.$channel;
			$sql .= $where . $order;
		}else{
			$sql .= $order;
		}
		$n_sql = 'SELECT COUNT(*) AS nums FROM foton20140107'.$where;
		$res = mysql_query($n_sql);
		$count = mysql_fetch_array($res);
		echo json_encode($count);
		break;
		
	case 7:
	$channel=empty($_POST['channel'])?'':trim($_POST['channel']);
	$start=empty($_POST['start'])?0:trim($_POST['start']);
	$page_size=empty($_POST['page_size'])?0:trim($_POST['page_size']);
	$sql = 'SELECT * FROM foton20140107';             
		$order = ' ORDER BY id DESC'; 
		$where='';
		if(!empty($channel)){
			$where .= ' WHERE channel = '.$channel;
			$sql .= $where . $order;
		}else{
			$sql .= $order;
		}
		$sql = $sql ." limit ".$start." , ".$page_size."";
		$res = mysql_query($sql);
echo  <<<HTML
	<html>
	<body>
		 
			<table width="100%" bgcolor="#D1DDAA" border = "1" cellpadding="2" cellspacing="1" style="margin-top:8px">
		<tr>
			<td width="5%">id</td>
			<td width="5%">用户名</td>
			<td width="10%">手机</td>
			<td width="8%">省份</td>
			<td width="8%">城市</td>
			<td width="8%">IP</td>
			<td width="8%">注册时间</td>
			<td width="8%">车型选择</td>
			<td width="8%">奖品</td>
			<td width="8%">Channel</td>
		</tr>
HTML;
		if($res){
				$body = '';
				while($row=mysql_fetch_assoc($res)){										
					echo "<tr>";
					echo "<td>{$row['id']}</td>";      
					echo "<td>{$row['username']}</td>";                              
					echo "<td>{$row['mobile']}</td>";                                                               			
					echo "<td>{$row['province']}</td>";                                                                			
					echo "<td>{$row['city']}</td>";
					echo "<td>{$row['ip']}</td>";                                                                   			
					echo "<td>{$row['logtime']}</td>";
					echo "<td>{$row['cartype']}</td>"; 
					echo "<td>{$row['jiangpin']}</td>";   
					echo "<td>{$row['channel']}</td>";   
					echo "</tr>";
				}
		}
		break;
	
	default:
		echo '非法访问';
}
?>